/**
 * 设置服务
 */
import { config } from '../config';

export class SettingsService {
  static SETTINGS_KEY = 'v2t_settings';

  static defaultSettings = {
    // 音频设置
    audio: {
      quality: 'medium', // low, medium, high
      autoStart: false,  // 自动开始转换
      maxDuration: config.audio.maxDuration
    },
    // 转换设置
    conversion: {
      language: 'zh',   // 目标语言
      autoSave: true,   // 自动保存历史
      showProgress: true // 显示进度
    },
    // 界面设置
    ui: {
      theme: 'light',   // light, dark
      fontSize: 14,     // 字体大小
      compactMode: false // 紧凑模式
    }
  };

  /**
   * 获取设置
   */
  static async getSettings() {
    try {
      const data = await chrome.storage.local.get(this.SETTINGS_KEY);
      return { ...this.defaultSettings, ...data[this.SETTINGS_KEY] };
    } catch (error) {
      console.error('获取设置失败:', error);
      return this.defaultSettings;
    }
  }

  /**
   * 更新设置
   * @param {Object} newSettings - 新设置
   */
  static async updateSettings(newSettings) {
    try {
      const currentSettings = await this.getSettings();
      const mergedSettings = { ...currentSettings, ...newSettings };
      
      await chrome.storage.local.set({
        [this.SETTINGS_KEY]: mergedSettings
      });
      
      return mergedSettings;
    } catch (error) {
      console.error('更新设置失败:', error);
      throw error;
    }
  }

  /**
   * 重置设置
   */
  static async resetSettings() {
    try {
      await chrome.storage.local.set({
        [this.SETTINGS_KEY]: this.defaultSettings
      });
      return this.defaultSettings;
    } catch (error) {
      console.error('重置设置失败:', error);
      throw error;
    }
  }
} 